/**
 * Created by easy on 2017/6/27.
 */
import axios from 'axios'
import { apiUrl, baseUrl } from '../api/config'

export const http = axios.create({
  baseURL: apiUrl
})

/**
 * http拦截器
 * */
  // 获取设备类型
const ua = navigator.userAgent

// http request 拦截器
http.interceptors.request.use(
  (config) => {
    // 获取accessToken
    const Token = window.localStorage.getItem('accessToken')
    if (Token) {
      // 设置Authorization
      config.headers.common['Authorization'] = 'Bearer ' + Token
    }
    return config
  },
  (error) => {
    if (error) {
      // 从localStorage移除accessToken和user，重新授权
      window.localStorage.removeItem('accessToken')
      window.localStorage.removeItem('user')
      if ((/MicroMessenger/i).test(ua)) {
        // 移动端授权页面
        window.location.href = apiUrl + '/api/oauth/weixin?redirect_uri=' + baseUrl + 'code'
      } else {
        // 网页授权页面
        window.location.href = apiUrl + '/api/oauth/weixinweb?redirect_uri=' + baseUrl + 'code'
      }
    }
    return error
  }
)

// http response 拦截器
http.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    const { response } = error
    if (!response) {
      return Promise.reject(error)
    }
    // 接口状态码为401时，从localStorage移除accessToken和user，重新授权
    if (response.status === 401) {
      window.localStorage.removeItem('accessToken')
      window.localStorage.removeItem('user')
      if ((/MicroMessenger/i).test(ua)) {
        window.location.href = apiUrl + '/api/oauth/weixin?redirect_uri=' + baseUrl + 'code'
      } else {
        window.location.href = apiUrl + '/api/oauth/weixinweb?redirect_uri=' + baseUrl + 'code'
      }
      return false
    }
    // 接口状态码为422时
    if (response.status === 422) {}
    // 接口状态码为404时
    if (response.status === 404) {}

    return Promise.reject(error)
  }
)

/**
 * 页面接口
 * */

const getData = obj => obj.data
// 当前用户
export const requestMe = (config = {}) => {
  let userInfo = window.localStorage.getItem('user')
  if (userInfo) {
    return new Promise((resolve, reject) => {
      resolve(JSON.parse(userInfo))
    })
  }
  return http.get(`/api/me`, config).then(getData).then(
    (resp) => {
      if (resp && resp.data) {
        let userInfo = resp.data
        window.localStorage.setItem('user', JSON.stringify(userInfo))
        return userInfo
      }
    }
  )
}
// 获取token
export const requestCode = (driver, code, config = {}) => { return http.get(`/api/oauth/${driver}/callback?code=${code}`, config) }
// 微信分享
export const requestJssdk = (detailId, config = {}) => { return http.get(`/api/wechat/jssdk?url=${encodeURIComponent(`${window.location.href.split('#')[0]}`)}`, config) }
// banner
export const requestBanner = (config = {}) => { return http.get(`/api/banners`, config) }
// StoreList
export const requestStoreList = (config = {}) => { return http.get(`api/stores`, config) }
// StoreDetail
export const requestStoreDetail = (id, config = {}) => { return http.get(`api/stores/${id}`, config) }
// 课程列表
export const requestSchedules = (id, config = {}) => { return http.get(`/api/schedules`, config) }
// 课程详情
export const requestSchedulesDetail = (id, config = {}) => { return http.get(`api/schedules/${id}`, config) }
// 支付
export const requestPay = (config = {}) => { return http.post(`/api/orders`, config) }
// 我的预约
export const requestOrders = (config = {}) => { return http.get(`/api/orders`, config) }

